package com.inspur.his.controller;

import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Dept;
import com.inspur.his.pojo.DeptExt;
import com.inspur.his.pojo.User;
import com.inspur.his.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

//科室管理部分（老师例子）
@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("deptListController")
    public String deptList(HttpServletRequest req, Model md) {
        // 获取页面传递过来的查询条件
        String name = req.getParameter("name");

        HashMap map = new HashMap<String, String>();
        map.put("name", name);
        List<DeptExt> deptList = deptService.deptList(map);
        md.addAttribute("deptList", deptList);
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("name", name);
        return "dept/deptList.jsp";
    }
    @RequestMapping("deptAddController")
    public String deptAdd(Dept dept, Model model) throws Exception {
        //参数绑定 user对象中已经绑定了表单中的用户信息(确认sql中插入的所有字段均有值吗？）
        //思考主键的生成方式--多种1 MySQL中的主键自增
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
        String today = fm.format(day);
        dept.setDepartid(today+String.valueOf(day.getTime()));
        dept.setCreatedate(day);
        dept.setStatus(Constant.USER_STATUS_ABLE);
        deptService.deptAdd(dept);
        model.addAttribute("dept",dept);
        model.addAttribute("flag",Constant.USER_STATUS_ABLE);
        model.addAttribute("success","科室信息保存成功！");
        return "dept/deptAdd.jsp";
    }

    @RequestMapping("deptList")
    @ResponseBody
    public List<DeptExt> deptInfo(HttpServletRequest req, Model md) {
        HashMap map = new HashMap<String, String>();
        String departid = req.getParameter("departid");
        map.put("departid", departid);
        List<DeptExt> deptList = deptService.deptList(map);
        md.addAttribute("deptList", deptList);
        return deptList;
    }
}
